package www.nupter.org.njuptask.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import org.xutils.http.RequestParams;

import java.util.ArrayList;
import java.util.List;

import www.nupter.org.njuptask.R;
import www.nupter.org.njuptask.adapter.CommonAdapter;
import www.nupter.org.njuptask.http.HttpCallBack;
import www.nupter.org.njuptask.http.HttpRequestUtil;
import www.nupter.org.njuptask.http.RequestUrl;
import www.nupter.org.njuptask.model.Questions;
import www.nupter.org.njuptask.utils.ViewHolder;


/**
 * A login screen that offers login via email/password.
 */
public class QuestionListActivity extends BaseActivity {

    private ListView listView;
    private CommonAdapter<Questions.DataEntity> commonAdapter;
    private List<Questions.DataEntity> questionsList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_question_list);
        listView = (ListView) findViewById(R.id.question_list);

        setRightText("发布");
        setTitle("问题列表");


        commonAdapter = new CommonAdapter<Questions.DataEntity>(this, questionsList, R.layout.question_item) {
            @Override
            public void convert(ViewHolder holder, Questions.DataEntity item, int position) {
                TextView titleText = holder.getView(R.id.title);
                TextView authorText = holder.getView(R.id.author_name);
                TextView content = holder.getView(R.id.des);
                titleText.setText(item.getTitle());
                authorText.setText(item.getName());
                content.setText(item.getDescription());
            }
        };

        listView.setAdapter(commonAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                Bundle bundle = new Bundle();
                bundle.putInt("id",questionsList.get(position).getId());
                bundle.putString("name",questionsList.get(position).getName());
                bundle.putString("content",questionsList.get(position).getContent());
                bundle.putString("file",questionsList.get(position).getFile());
                bundle.putString("title",questionsList.get(position).getTitle());
                bundle.putInt("questionId",questionsList.get(position).getId());
                Intent intent = new Intent();
                intent.putExtra("bun", bundle);
                intent.setClass(QuestionListActivity.this, QuestionDetailActivity.class);
                startActivity(intent);
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i("question", "执行到这里了");
        getQuestions();
    }

    public void getQuestions() {
        RequestParams requestParams = new RequestParams(RequestUrl.list_question);
        HttpRequestUtil.getMethod(requestParams, Questions.class, new HttpCallBack<Questions>() {
            @Override
            public void success(Questions data) {
                questionsList.clear();
                questionsList.addAll(data.getData());
                commonAdapter.notifyDataSetChanged();
            }
            @Override
            public void erro(String res) {
                BaseToast(res);
            }
        });
    }


    @Override
    protected void HandleTitleBarEvent(TitleBar component, View v) {
        if (component == TitleBar.LEFT) {
            finish();
        } else if (component == TitleBar.RightText) {
            Intent intent = new Intent();
            intent.setClass(QuestionListActivity.this, PublishQuestionActivity.class);
            startActivity(intent);
        }
    }
}

